
/* adm channel data */
var admChannelDataMap = new HashMap();

function addChannel(channel, data) {
	admChannelDataMap.put(channel, data);
}

function getChannelData(channel) {
	return admChannelDataMap.get(channel);
}

function removeChannel(channel) {
	admChannelDataMap.remove(channel);
}

/* admAccount */
var admAccounts = new HashMap();

function addAdmAccount(admAccount) {
	admAccounts.put(admAccount.name, admAccount);
}

function removeAdmAccount(admName) {
	admAccounts.remove(admName);
}

function findAdmAccount(admName) {
	return admAccounts.get(admName);
}

addAdmAccount(new AdmAccount("adm", "adm"));
addAdmAccount(new AdmAccount("test", "test"));

/* admCmds */
var admCmds = new HashMap();

function defAdmCmd(adm) {
	admCmds.put(adm.name, adm);
}

function findAdmCmd(name) {
	return admCmds.get(name);
}

function handleAdmConnect(ctx) {
	ctx.writeAndFlush("please login first\r\nlogin <accountname>\r\n>");
}

function admPrintCmdStartTip(ctx) {
	var data = getChannelData(ctx.channel());
	if (data && data.get(AdmChannelDataConsts.CurCmd)) {
		ctx.writeAndFlush(">>");
	} else {
		ctx.writeAndFlush(">");
	}
}

function handleAdmCmd(cmdLine, ctx) {
	var channel = ctx.channel();
	var logined = false;
	var data = getChannelData(channel);
	var parser = new StringParser(cmdLine);
	parser.setWithDefaultWps(true);
	var brackets = new CharArray(2);
	brackets[0] = brackets[1] = '"';
	parser.setBrackets(brackets);
	//
	if (data) {
		var curCmd = data.get(AdmChannelDataConsts.CurCmd);
		if (curCmd) {
			curCmd.executeContinue(parser, ctx);
			admPrintCmdStartTip(ctx);
			return;
		}
		logined = data.getBoolean(AdmChannelDataConsts.Logined, false);
	}
	var cmdName = parser.nextToken();
	var admCmd = findAdmCmd(cmdName);
	if (!admCmd) {
		ctx.writeAndFlush("can't find cmd\r\n>");
		return;
	}
	if (logined || !admCmd.needLogin()) {
		admCmd.execute(parser, ctx);
		admPrintCmdStartTip(ctx);
	} else {
		ctx.writeAndFlush("please login first\r\n>");
	}
}